// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const path = require('path');
const packageJSON = require('./package.json');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const DefinePlugin = require('webpack/lib/DefinePlugin');

module.exports = {
    mode: 'development',
    resolve: {
        extensions: ['.ts', '.js', '.json', '.wasm'],
        modules: [
            '../fork',
            'node_modules'
        ]
    },
    devServer: {
        hot: true,
        open: true, // 自动打开浏览器
        openPage: 'http://localhost:8080/enter.html' // 默认打开的页面路径
    },
    resolveLoader: {
        modules: [
            'webpack-tools',
            'node_modules'
        ]
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: {
                    loader: 'ts-loader'
                }
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /opus\.min\.js$/,
                use: {
                    loader: 'raw-loader'
                }
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new DefinePlugin({
            '__APP_VERSION__': JSON.stringify(packageJSON.version),
            '__IS_DEBUG__': 'true',
            '_HTTP_MODE_': 'false',
            '_EXTRA_UI_': 'true',
        }),
        new HtmlWebpackPlugin({
            template: 'login.html',
            filename: 'login.html',
            inject: false
        }),
        new HtmlWebpackPlugin({
            template: 'enter.html',
            filename: 'enter.html',
            inject: false
        }),
        new HtmlWebpackPlugin({
            template: 'phone.html',
            filename: 'phone.html',
            inject: false
        }),
        new HtmlWebpackPlugin({
            template: 'demo/demo.html',
            filename: 'demo.html',
            inject: 'head'
        }),
        new CopyWebpackPlugin([{
            from: 'demo/demo.md',
            to: path.join(__dirname, '/dist/demo.md'),
            force: true
        }, {
            from: 'src/3rd/video-codec-lib/',
            to: path.join(__dirname, '/dist/lib'),
            force: true
        },{
            from: 'images/',
            to: path.join(__dirname, '/dist/images/'),
            force: true
        },{
            from: 'style/',
            to: path.join(__dirname, '/dist/style/'),
            force: true
        },{
            from: 'common.js',
            to: path.join(__dirname, '/dist/common.js'),
            force: true
        }, {
            from: 'common.json',
            to: path.join(__dirname, '/dist/common.json'),
            force: true
        }])
    ],
    entry: {
        CloudApp: './src/CloudApp.js',
        StartScreenPrint: './src/StartScreenPrint.js',
    },
    output: {
        libraryTarget: 'umd',
        library: '[name]',
        libraryExport: 'default',
        umdNamedDefine: true,
        path: path.join(__dirname, '/dist/'),
        filename: './sdk/[name].min.js',
        chunkFilename: './sdk/[name].bundle.js'
    }
};
